package com.xulinhao.filter;/**
 * @author xulinhao
 * @create 2021-06-02 21:44
 */

import com.xulinhao.utils.JdbcUtils;

import javax.servlet.*;
import java.io.IOException;
import java.rmi.RemoteException;

/**
 * @Author xlh
 * @packagename: com.xulinhao.filter
 * @classname: TransactionFilter
 * @Description:
 * @Date 2021/6/2
 */
public class TransactionFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        try {
            filterChain.doFilter(servletRequest, servletResponse);
            JdbcUtils.commitAndClose();
        } catch (Exception e) {
            JdbcUtils.rollbackAndClose();
            e.printStackTrace();
            //把异常抛给Tomcat管理展示友好的错误页面
            throw new RuntimeException(e);
        }

    }

    @Override
    public void destroy() {

    }
}
